class Common {
    hideScroll() {
        const {scrollHeight, clientHeight} = document.documentElement
        if (scrollHeight > clientHeight) {
            document.body.style.paddingRight = '17px'
            document.body.style.overflow = 'hidden'
        }
    }

    showScroll() {
        document.body.style.paddingRight = ''
        document.body.style.overflow = ''
    }

    debounce(func, wait, immediate) {
        var timeout;
        return function () {
            var context = this, args = arguments;
            if (args && args.length > 0) {//保持事件
                if (args[0].persist) {
                    args[0].persist();
                }
            }
            var later = function () {
                timeout = null;
                if (!immediate) func.apply(context, args);
            };
            var callNow = immediate && !timeout;
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
            if (callNow) func.apply(context, args);
        }
    }

    /**
     * 判断指定容器的,指定内容是否超出屏幕底部
     * @param container 容器
     * @param content  内容(选择器或者Dom)
     * @returns {boolean} 是否超出
     */
    isOverPage(container, content) {
        const {top, height} = container.getBoundingClientRect()
        content = typeof content === 'string' ? container.querySelector(content) : content
        return (top + height + content.offsetHeight) > document.documentElement.clientHeight + window.scrollY
    }

    toSymbol(value) {
        let _value = value
        if (typeof value !== "symbol") {
            _value = Symbol.for(value)
        }
        return _value
    }

    toPx(value, parentSize) {
        if (!value) {
            return 0
        }
        if (typeof value === 'number') {
            return value
        }
        if (value.endsWith('px')) {
            return parseFloat(value)
        }
        if (value.endsWith('%') && parentSize) {
            return parseFloat(value) / 100 * parentSize
        }
        return value
    }
}

export default new Common()